#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long
#define piii pair<ll,pair<ll,ll> >
#define pi first
#define p1 second.first
#define p2 second.second
#define mp(a,b,c) make_pair(a,make_pair(min(b,c),max(b,c)))
//priority_queue<int,vector<int> >
//next_permutation

int main()
{
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    int t;
    cin >> t;
    while (t--)
    {
        map<piii,ll>f;
        map<piii,bool>vis;
        int n;
        cin >> n;
        vector<int>a(n+1);
        for (int i = 1; i <= n; i++) scanf("%d",&a[i]);
        f[mp(1,a[0],-1)] = 0;
        f[mp(1,-1,a[0])] = 0;
        vis[mp(1,a[0],-1)] = 1;
        vis[mp(1,-1,a[0])] = 1;
        queue<piii>q;
        q.push(mp(1,a[1],-1));
        q.push(mp(1,-1,a[1]));
        ll ans = 0;
        while (q.size())
        {
            piii x = q.front();
            q.pop();
            ll i = x.pi, r = x.p1, b = x.p2;
            ll u = a[i+1];
            if (r == -1)
            {
                q.push(mp(i+1,u,b));
                vis[mp(i+1,u,b)] = 1;
                continue;
            }
            if (b == -1)
            {
                q.push(mp(i+1,r,u));
                vis[mp(i+1,r,u)] = 1;
                continue;
            }
            if (u == r)
            {
                f[mp(i+1,u,b)] = max(f[mp(i+1,u,b)],f[mp(i,r,b)]+u);
            }
            else f[mp(i+1,u,b)] = max(f[mp(i,r,b)],f[mp(i+1,u,b)]);
            if (u == b)
            {
                f[mp(i+1,r,u)] = max(f[mp(i+1,r,u)],f[mp(i,r,b)]+u);
            }
            else f[mp(i+1,r,u)] = max(f[mp(i+1,r,u)],f[mp(i,r,b)]);
            if (i+1 == n)
            {
                ans = max(ans, max(f[mp(i+1,u,b)],f[mp(i+1,r,u)]));
            }
            if (!vis[mp(i+1,u,b)] && i+1 <= n)
            {
                q.push(mp(i+1,u,b));
                vis[mp(i+1,u,b)] = 1;
            }
            if (!vis[mp(i+1,r,u)] && i+1 <= n)
            {
                q.push(mp(i+1,r,u));
                vis[mp(i+1,r,u)] = 1;
            }
        }
        cout << ans << endl;
    }
    return 0;
}
